WP_currentGuideID = 1
WP_currentStep = 1
WP_currentType = "other"
WP_currentGuide = nil
WP_currentX = 0
WP_currentY = 0
WP_currentEvents = {}
WP_lastDistance = -1
WP_TomTom = false
WP_lastwaypoint = 0

BINDING_HEADER_JAMESADDON = "WoW-Pro Addon"

function WP_Initialize()
	SlashCmdList["JAMESADDONSLASH"] = WP_SlashHandler
	SLASH_JAMESADDONSLASH1 = "/jamesaddon"
	SLASH_JAMESADDONSLASH2 = "/ja"
	this:RegisterEvent("ADDON_LOADED")
	this:RegisterEvent("CHAT_MSG_SYSTEM")
	this:RegisterEvent("CHAT_MSG_LOOT")
	this:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	this:RegisterEvent("QUEST_LOG_UPDATE")
	this:RegisterEvent("ZONE_CHANGED")
	this:RegisterEvent("ZONE_CHANGED_INDOORS")
	this:RegisterEvent("ZONE_CHANGED_NEW_AREA")
	this:RegisterEvent("PLAYER_XP_UPDATE")
	this:RegisterEvent("PLAYER_LEVEL_UP")
	this:RegisterEvent("TAXIMAP_OPENED")
	this:RegisterEvent("MERCHANT_SHOW")
	this:RegisterEvent("GOSSIP_SHOW")
	this:RegisterEvent("TRAINER_SHOW")
	this:RegisterEvent("UPDATE_FACTION")
end 

function WP_SlashHandler(msg)
	if WP_MidFrame:IsVisible() then
		WP_MidFrame:Hide()
	else
		WP_MidFrame:Show()
	end
end

function WP_NextStep()
	if WP_currentStep < table.getn(WP_currentGuide.guide) then
		WP_currentStep = WP_currentStep + 1
		while WP_CheckStep(WP_currentGuide.guide[WP_currentStep][1]) == false and WP_currentStep < table.getn(WP_currentGuide.guide) do
			WP_currentStep = WP_currentStep + 1
		end
		WP_ShowStep()
	end
end 

function WP_PrevStep()
	if WP_currentStep > 1 then
		WP_currentStep = WP_currentStep - 1
		while WP_CheckStep(WP_currentGuide.guide[WP_currentStep][1]) == false and WP_currentStep > 1 do
			WP_currentStep = WP_currentStep - 1
		end
		WP_ShowStep()
	end
end 

function WP_ShowStep()
	WP_currentEvents ={}
	WP_RegisterStepEvents(WP_currentGuide.guide[WP_currentStep][1])
	if WP_currentEvents[1] ~= nil then WP_Event:Show() else WP_Event:Hide() end
	WP_MidFrame_Text:SetText(WP_replace(WP_currentGuide.guide[WP_currentStep][1]))
	WP_currentX = WP_currentGuide.guide[WP_currentStep][2]
	WP_currentY = WP_currentGuide.guide[WP_currentStep][3]
	
	if WP_TomTom == true then
		WP_TomTomSet()
		-- WP_currentX, WP_currentY = 0, 0
	end
	
	WP_BotFrame_Edit:SetText(WP_currentStep)
	WP_lastDistance = -1
end


function WP_SetGuide(t,id,step)
	if t == "horde" then
		WP_currentGuide = WP_guides_H[id]
		WP_TopFrame_Type:SetTexture("Interface\\PvPRankBadges\\PvPRankHorde")
	elseif t == "alliance" then
		WP_currentGuide = WP_guides_A[id]
		WP_TopFrame_Type:SetTexture("Interface\\PvPRankBadges\\PvPRankAlliance")
	elseif t == "both" then
		WP_currentGuide = WP_guides_B[id]
		WP_TopFrame_Type:SetTexture("Interface\\FriendsFrame\\FriendsFrameScrollIcon")
	elseif t == "other" then
		WP_currentGuide = WP_guides_O[id]
		WP_TopFrame_Type:SetTexture("Interface\\CharacterFrame\\TempPortrait")
	end
	
	WP_currentType = t
	WP_currentGuideID = id
	WP_currentStep = step - 1
	
	if WP_currentGuide == nil then 
		WP_currentGuide = B_INDEX_GUIDE 
	end
	
	WP_TopFrame_Header:SetText(WP_currentGuide.header);

	WP_NextStep();
end 

function WP_Update()
	-- Position incl formatting
	--local px,py = GetPlayerMapPosition("player")
	--local pxl = floor(px*100,0)
	--local pxr = floor((px*100-pxl)*100,0)
	--local pyl = floor(py*100,0)
	--local pyr = floor((py*100-pyl)*100,0)
	--WP_TopFrame_Position:SetText("Position |cFFFFFF00("..pxl.."."..pxr.."|"..pyl.."."..pyr..")")
	
	WP_CheckRepairEvent()
	WP_CheckAllLootEvents()
	WP_CheckSkillEvent()
	
	if WP_currentX > 0 and WP_currentY > 0 then
		if not WP_TomTom then
			if not WP_TopFrame_Arrow:IsVisible() then WP_TopFrame_Arrow:Show() end
			if not WP_TopFrame_DistanceBar:IsVisible() then WP_TopFrame_DistanceBar:Show() end
			if not WP_TopFrame_DistanceBarBorder:IsVisible() then WP_TopFrame_DistanceBarBorder:Show() end
			WP_TopFrame_Distance:SetText((floor(WP_GetDistance(WP_currentX,WP_currentY)*10000)))
		end
		WP_SetArrow(WP_currentX,WP_currentY)
		if WP_lastDistance == -1 then WP_lastDistance = WP_GetDistance(WP_currentX,WP_currentY) end
		WP_SetDistanceBar()
	else
		if WP_lastDistance ~= -1 then WP_lastDistance = -1 end
		if WP_TopFrame_Arrow:IsVisible() then WP_TopFrame_Arrow:Hide() end
		if WP_TopFrame_Arrival:IsVisible() then WP_TopFrame_Arrival:Hide() end
		if WP_TopFrame_DistanceBar:IsVisible() then WP_TopFrame_DistanceBar:Hide() end
		if WP_TopFrame_DistanceBarBorder:IsVisible() then WP_TopFrame_DistanceBarBorder:Hide() end
		WP_TopFrame_Distance:SetText("")
	end
end 	
	
function WP_replace(str)
	local s = string.gsub(str, "!G", "|TInterface\\GossipFrame\\AvailableQuestIcon:0|t")
	s = string.gsub(s, "!T", "|TInterface\\GossipFrame\\ActiveQuestIcon:0|t")
	s = string.gsub(s, "!K", "|TInterface\\GossipFrame\\battleMasterGossipIcon:0|t")
	s = string.gsub(s, "!L", "|TInterface\\GossipFrame\\VendorGossipIcon:0|t")
	s = string.gsub(s, "!M", "|TInterface\\GossipFrame\\TaxiGossipIcon:0|t")
	s = string.gsub(s, "!U", "|TInterface\\GossipFrame\\BinderGossipIcon:0|t")
	s = string.gsub(s, "!S", "|TInterface\\GossipFrame\\GossipGossipIcon:0|t")
	s = string.gsub(s, "!H", "|TInterface\\Icons\\INV_Misc_Rune_01:0|t")
	s = string.gsub(s, "%[G%]", "|TInterface\\GossipFrame\\AvailableQuestIcon:0|t")
	s = string.gsub(s, "%[T%]", "|TInterface\\GossipFrame\\ActiveQuestIcon:0|t")
	s = string.gsub(s, "%[K%]", "|TInterface\\GossipFrame\\battleMasterGossipIcon:0|t")
	s = string.gsub(s, "%[L%]", "|TInterface\\GossipFrame\\VendorGossipIcon:0|t")
	s = string.gsub(s, "%[M%]", "|TInterface\\GossipFrame\\TaxiGossipIcon:0|t")
	s = string.gsub(s, "%[U%]", "|TInterface\\GossipFrame\\BinderGossipIcon:0|t")
	s = string.gsub(s, "%[S%]", "|TInterface\\GossipFrame\\GossipGossipIcon:0|t")
	s = string.gsub(s, "%[H%]", "|TInterface\\Icons\\INV_Misc_Rune_01:0|t")
	s = string.gsub(s,"%[color=(%x*)%]","|cFF%1") 
	s = string.gsub(s,"%[/color%]","|r") 
	s = string.gsub(s,"%[quest%]","|cFFFFFF00") 
	s = string.gsub(s,"%[/quest%]","|r") 
	s = string.gsub(s,"%[location%]","|cFF33cc00") 
	s = string.gsub(s,"%[/location%]","|r") 
	s = string.gsub(s,"%[objective%]","|cFFFF0000") 
	s = string.gsub(s,"%[/objective%]","|r") 
	s = string.gsub(s,"%[item%]","|cFFFFAA44") 
	s = string.gsub(s,"%[/item%]","|r") 
	s = string.gsub(s,"%[npc%]","|cFF4444FF") 
	s = string.gsub(s,"%[/npc%]","|r") 
	s = string.gsub(s,"%[coordinate%]","|cFF999999") 
	s = string.gsub(s,"%[/coordinate%]","|r") 
	s = string.gsub(s,"%[q%]","|cFFFFFF00") 
	s = string.gsub(s,"%[/q%]","|r") 
	s = string.gsub(s,"%[l%]","|cFF33cc00") 
	s = string.gsub(s,"%[/l%]","|r") 
	s = string.gsub(s,"%[o%]","|cFFFF0000") 
	s = string.gsub(s,"%[/o%]","|r") 
	s = string.gsub(s,"%[i%]","|cFFFFAA44") 
	s = string.gsub(s,"%[/i%]","|r") 
	s = string.gsub(s,"%[n%]","|cFF4444FF") 
	s = string.gsub(s,"%[/n%]","|r") 
	s = string.gsub(s,"%[c%]","|cFF999999") 
	s = string.gsub(s,"%[/c%]","|r")
	s = string.gsub(s,"%<.->","") 
	return s
end 

function WP_EnterStep()
	local num = WP_BotFrame_Edit:GetNumber()
	
	if num < table.getn(WP_currentGuide.guide) then
		WP_currentStep = num - 1
		WP_NextStep()
	else
		WP_BotFrame_Edit:SetNumber(WP_currentStep)
	end
	WP_BotFrame_Edit:ClearFocus()
end 

function WP_SetDistanceBar()
	local dist = WP_GetDistance(WP_currentX,WP_currentY)
	if dist > WP_lastDistance then
		WP_lastDistance = dist
	end
	local len = (dist / WP_lastDistance) * 144
	WP_TopFrame_DistanceBar:SetWidth(len)
	
end 

function WP_Options_OnLoad(panel)
	panel.name = "WoW-Pro Addon"
	InterfaceOptions_AddCategory(panel);
end

function WP_Options_Transparency_OnValueChanged(self)
	WP_MidFrame:SetAlpha(1-self:GetValue())
end 

function WP_Tooltip()
	local px,py = GetPlayerMapPosition("player")
	local pxl = floor(px*100,0)
	local pxr = floor((px*100-pxl)*100,0)
	local pyl = floor(py*100,0)
	local pyr = floor((py*100-pyl)*100,0)

	GameTooltip_SetDefaultAnchor(GameTooltip,UIParent)
	GameTooltip:AddLine("Current Target:")
	if WP_currentX == 0 and WP_currentY == 0 then GameTooltip:AddLine("none",.85,.85,.85,1)
	else GameTooltip:AddLine("("..(WP_currentX*100).."|"..(WP_currentY*100)..")",.85,.85,.85,1) end
	GameTooltip:AddLine()
	GameTooltip:AddLine("Current Position:")
	GameTooltip:AddLine("("..pxl.."."..pxr.."|"..pyl.."."..pyr..")",.85,.85,.85,1)
	GameTooltip:Show()
end 

function WP_ChooseTomTom()
	if TomTom then
		if WP_TomTom == false then WP_TomTom = true else 
			WP_TomTom = false 
			if WP_lastwaypoint then TomTom:RemoveWaypoint(WP_lastwaypoint) end
		end
	else
		WP_Debug(0, "Did not find the TomTom Addon")
	end
	WP_ShowStep()
end

function WP_TomTomSet()
	if TomTom then
		if WP_lastwaypoint then TomTom:RemoveWaypoint(WP_lastwaypoint) end
		if WP_currentX > 0 and WP_currentY > 0 then
			WP_lastwaypoint = TomTom:AddZWaypoint(GetCurrentMapContinent(), GetCurrentMapZone(), (WP_currentX*100), (WP_currentY*100), "", false)
		end	 
	end
end 